package cn.duhengkui.national.valuelist.masterslavedb;


import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;

import cn.duhengkui.national.valuelist.annotation.DataSourceType;
import cn.duhengkui.national.valuelist.context.DBContextHolder;

public class TransactionManager extends DataSourceTransactionManager{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = -3320043080774041074L;
	
	/* (non-Javadoc)
	 * @see org.springframework.jdbc.datasource.DataSourceTransactionManager#doBegin(java.lang.Object, org.springframework.transaction.TransactionDefinition)
	 */
	@Override
    protected void doBegin(Object transaction, TransactionDefinition definition) {
 
        //设置数据源
        boolean readOnly = definition.isReadOnly();
        if(readOnly) {
        	DBContextHolder.setContextHolder(DataSourceType.Type.Read);
        } else {
        	DBContextHolder.setContextHolder(DataSourceType.Type.Write);
        }
        super.doBegin(transaction, definition);
        
    }
	

 

    @Override
    protected void doCleanupAfterCompletion(Object transaction) {
        super.doCleanupAfterCompletion(transaction);
        DBContextHolder.setContextHolder(null);
        
    }

}
